// components/home/tools/index.js
import {
  remove_list
} from '../../../api/collection_record'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ids: {
      type: Array,
      value: [],
      observer(data) {
        let ids = this.properties.dataSource.filter(it => it.status !== 1).map(it => it.id);
        if (ids.length > data.length) {
          this.setData({
            checked: false
          })
        } else if (ids.length === data.length && ids.length > 0) {
          this.setData({
            checked: true
          })
        }
      }
    },
    dataSource: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    checked: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      this.setData({
        checked: event.detail,
      });
      if (event.detail) {
        let ids = this.properties.dataSource.filter(it => it.status !== 1).map(it => it.id);
        this.triggerEvent('change', ids);
      } else {
        this.triggerEvent('change', []);
      }
    },
    onRemove() {
      if (this.properties.ids.length < 1) {
        wx.showModal({
          showCancel: false,
          content: '可以通过点击右边的列表来选择要删除的选项!\r [已完成]的状态不可选中',
          title: '提示'
        })
        return
      }
      wx.showModal({
        title: "提示",
        content: "确定要删除这些记录吗？",
        success: res => {
          if (res.confirm) {
            remove_list({
              ids: this.properties.ids
            }, () => {
              this.triggerEvent('remove', this.properties.ids);
            })
          }
        }
      })
    },
    onCancel(){
      this.triggerEvent('cancel');
    }
  }
})